/**
 * 厦门大学计算机专业 | 华为开发专家(HDE)
 * 专注《零基础学编程系列》  http://lblbc.cn/blog
 * 包含：鸿蒙 | Java | 安卓 | 前端 | Flutter | iOS | 小程序
 * 公众号：蓝不蓝编程
 */
class Block {
  constructor(public shape: number[][], public position: Position, public rotation: number = 0) {
    this.shape = shape;
    this.position = position;
    this.rotation = rotation;
    this.lastShape = []; // 用于存储上一次的形状
    this.lastRotation = 0; // 用于存储上一次的旋转状态
  }

  private lastShape: number[][]; // 用于存储上一次的形状
  private lastRotation: number; // 用于存储上一次的旋转状态

  // 正确的旋转形状逻辑
  rotate(): void {
    // 保存上一次的状态
    this.lastShape = JSON.parse(JSON.stringify(this.shape));
    this.lastRotation = this.rotation;
    if (this.rotation === 3) {
      this.rotation = 0; // 重置为0
    } else {
      this.rotation += 1; // 增加旋转角度
    }

    // 根据当前旋转角度来旋转形状
    const newShape: number[][] = [];
    for (let i = 0; i < this.shape[0].length; i++) {
      newShape.push([]);
      for (let j = this.shape.length - 1; j >= 0; j--) {
        newShape[i].push(this.shape[j][i]);
      }
    }
    this.shape = newShape;
  }

  // 撤销旋转
  undoRotate(): void {
    if (this.lastShape.length > 0) {
      this.shape = this.lastShape;
      this.rotation = this.lastRotation;
      this.lastShape = []; // 清空上一次的状态
    } else {
      console.log('No rotation history to undo');
    }
  }

  // 移动形状
  move(dx: number, dy: number): void {
    this.position.x += dx;
    this.position.y += dy;
  }
}

// 定义七种基本形状
const shapes: Block[] = [
  new Block([
    [2, 0],
    [0, 2],
    [2, 2]
  ], {x: 0, y: 0}), // J
  new Block([
    [0, 2],
    [2, 2],
    [2, 0]
  ], {x: 0, y: 0}), // S
  new Block([
    [2, 2],
    [2, 2]
  ], {x: 0, y: 0}), // O
  new Block([
    [0, 2, 0],
    [2, 2, 2],
    [0, 0, 0]
  ], {x: 0, y: 0}), // T
  new Block([
    [2, 2, 2],
    [0, 0, 2]
  ], {x: 0, y: 0}), // L
  new Block([
    [2, 2, 2],
    [2, 0, 0]
  ], {x: 0, y: 0}), // I
  new Block([[2, 2, 2, 2]], {x: 0, y: 0}) // Z
];

interface Position {
  x: number;
  y: number;
}

export { Block, shapes , Position};
